#include <time.h>
#include <sys/time.h>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include "astar.h"

using namespace std;
using namespace LVGame;

#define MAP_WIDTH 200
#define MAP_HEIGHT 100

void printTime()
{
    char buff[128];
    char time_string[128];

    struct timeval t;
    gettimeofday(&t, NULL);

    strftime(buff, sizeof(buff), "TIME:%Y-%m-%d_%H:%M:%S", localtime(&t.tv_sec));
    snprintf(time_string, sizeof(time_string), "%s.%03ld", buff, t.tv_usec / 1000);

    cout << time_string << "\n";
}

void loadSearchMap(const string& map_name, vector<int>* search_map)
{
    ifstream fin(map_name.c_str());
    if (!fin) {
        exit(1);
    }

    char input;
    while (fin >> input) {
        if ('0' == input) {
            search_map->push_back(0);
        } else if ('1' == input) {
            search_map->push_back(1);
        }
    }
}

int main()
{
    /*
    int search_map[100] = {
        0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 1, 0, 1, 0, 0, 0,
        0, 0, 0, 0, 1, 0, 1, 0, 0, 0,
        0, 0, 0, 0, 1, 0, 1, 0, 0, 0,
        0, 0, 0, 0, 1, 0, 1, 0, 0, 0,
        0, 0, 0, 0, 1, 0, 1, 0, 0, 0,
        0, 0, 0, 0, 1, 0, 1, 0, 0, 0,
        0, 0, 0, 0, 1, 0, 1, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 1, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 1, 0, 0, 0,
    };
    */
    vector<int> search_map;
    loadSearchMap("1.map", &search_map);

    printTime();
    AStar astar;
    vector<Point> result_path;
    astar(&search_map[0], MAP_WIDTH, MAP_HEIGHT, Point(0, 0), Point(199, 99), &result_path);
    printTime();

    for (size_t i = 0; i < result_path.size(); ++i) {
        search_map[MAP_WIDTH * result_path[i].y + result_path[i].x] = 2;
    }

    for (size_t i = 0; i < MAP_HEIGHT; ++i) {
        for (size_t j = 0; j < MAP_WIDTH; ++j) {
            cout << search_map[i * MAP_WIDTH + j] << "";
        }
        cout << "\n";
    }

    return 0;
}
